<?php
namespace app\admin\controller;

use app\admin\model\AdminRole;
use app\admin\model\AdminUser;
use app\admin\validate\IDVali;
use app\admin\validate\UserVali;
use think\Log;

class Account extends Base
{
    public function index() {
        $data = AdminUser::getAdminUserList();

        $this->assign( 'list', $data );
        $this->assign('page', $data->render());

//        Log::write( $data->toArray(),'print');
        /*$test = 'liwenlong';
        function func (){
            global $test;
            dump($test);
        }
        return func();*/
        return $this->fetch();
    }

    // 添加
    function add () {
        $role = AdminRole::getAdminRoleList();
//        Log::write( $role->toArray(),'print');

        $this->assign( 'list', $role );

        return $this->fetch();
    }

    function doAdd() {
        $req = (new UserVali())->goCheck('add');
        $req['password'] = empty($req['password']) ? '123456' : $req['password'];

        $adminRole = new AdminUser();
        $adminRole->allowField(true)->save( $req );

        return json( array( 'error_code' => 1, 'msg' => '操作成功', 'data' => $req ) );
    }

    // 修改
    function edit () {
        $req = (new IDVali())->goCheck();
        $data = AdminUser::getAdminUserById( $req['id'] );
//        Log::write( $data->toArray(),'print');

        $this->assign('data', $data);
        $this->assign('role', AdminRole::getAdminRoleList());
        return $this->fetch();
    }

    function doEdit() {
        $req = (new UserVali())->goCheck('edit');

//        Log::write( $req,'print');
        $req['password'] = empty($req['password']) ? '123456' : $req['password'];
        $adminRole = new AdminUser();
        $adminRole->allowField(true)->save( $req, array('admin_id' => $req['id']) );

        return json( array( 'error_code' => 1, 'msg' => '操作成功', 'data' => $req ) );
    }

    // 删除
    function del() {
        $req = (new IDVali())->goCheck();

        AdminUser::delUserByID( $req['id'] );

        return json( array( 'error_code' => 1, 'msg' => '操作成功', 'data' => $req ) );
    }

}
